package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.CustomerOrder;
import org.example.mapper.CustomerOrderMapper;
import org.example.service.CustomerOrderService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 客户订单服务实现类
 */
@Service
public class CustomerOrderServiceImpl extends ServiceImpl<CustomerOrderMapper, CustomerOrder> implements CustomerOrderService {
    
    @Override
    public CustomerOrder getByOrderNo(String orderNo) {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        return getOne(queryWrapper);
    }
    
    @Override
    public List<CustomerOrder> getByPlName(String plName) {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("pl_name", plName);
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerOrder> getByPlPhone(String plPhone) {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pl_phone", plPhone);
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerOrder> getByStatus(String status) {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerOrder> getByLoanAmountRange(BigDecimal minAmount, BigDecimal maxAmount) {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        if (minAmount != null) {
            queryWrapper.ge("loan_amount", minAmount);
        }
        if (maxAmount != null) {
            queryWrapper.le("loan_amount", maxAmount);
        }
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerOrder> getByLoanDateRange(LocalDate startDate, LocalDate endDate) {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        if (startDate != null) {
            queryWrapper.ge("fr_time", startDate.toString());
        }
        if (endDate != null) {
            queryWrapper.le("fr_time", endDate.toString());
        }
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerOrder> getByCity(String cityName) {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sale_city_name", cityName);
        return list(queryWrapper);
    }
    
    @Override
    public BigDecimal getTotalLoanAmount() {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("SUM(loan_amount) as totalAmount");
        List<CustomerOrder> list = list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            return list.get(0).getLoanAmount();
        }
        return BigDecimal.ZERO;
    }
    
    @Override
    public BigDecimal getOutstandingAmount() {
        QueryWrapper<CustomerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未结清");
        queryWrapper.select("SUM(remain_pay_amount) as outstandingAmount");
        List<CustomerOrder> list = list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            return list.get(0).getRemainPayAmount();
        }
        return BigDecimal.ZERO;
    }
}
